﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Truextend.CoffeShop.NewProvider
{
    public partial class NewProviderUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBind();                                
            }
        }

        private void DataBind()
        {

            DataTable dt = new DataTable();
            
            dt.Columns.Add(new DataColumn("Product", typeof(string)));
            dt.Columns.Add(new DataColumn("UnitPrice", typeof(string)));                                                

            GridviewProducts.DataSource = dt;            
            GridviewProducts.DataBind();
        }

        private void AddNewRowToGrid()
        {

            if (ViewState["CurrentTable"] != null)
            {                

                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {                    
                        //extract the TextBox values
                    string product = ((TextBox)GridviewProducts.FooterRow.FindControl("TxBxNewProduct")).Text;
                    string unitPrice = ((TextBox)GridviewProducts.FooterRow.FindControl("TxBxNewUnitPrice")).Text;

                    drCurrentRow = dtCurrentTable.NewRow();

                    drCurrentRow["Product"] = product;
                    drCurrentRow["UnitPrice"] = unitPrice;                                                

                    //add new row to DataTable
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    //Store the current data to ViewState
                    ViewState["CurrentTable"] = dtCurrentTable;

                    //Rebind the Grid with the current data
                    GridviewProducts.DataSource = dtCurrentTable;
                    GridviewProducts.DataBind();
                }
                
            }
            
            
        }
        
        protected void LinkBtnAddProduct_Click(object sender, EventArgs e)
        {
            if(GridviewProducts.Rows.Count > 0)
                AddNewRowToGrid();
            else
            {
                Control control = GridviewProducts.Controls[0].Controls[0];

                string product = ((TextBox)control.FindControl("TxBxEmptyProduct")).Text;
                string unitPrice = ((TextBox)control.FindControl("TxBxEmptyUnitPrice")).Text;

                DataTable dtCurrentTable = new DataTable();

                dtCurrentTable.Columns.Add(new DataColumn("Product", typeof(string)));
                dtCurrentTable.Columns.Add(new DataColumn("UnitPrice", typeof(string)));

                DataRow drCurrentRow = dtCurrentTable.NewRow();

                drCurrentRow["Product"] = product;
                drCurrentRow["UnitPrice"] = unitPrice;

                //add new row to DataTable
                dtCurrentTable.Rows.Add(drCurrentRow);
                //Store the current data to ViewState
                ViewState["CurrentTable"] = dtCurrentTable;

                //Rebind the Grid with the current data
                GridviewProducts.DataSource = dtCurrentTable;
                GridviewProducts.DataBind();

            }
        }

        protected void GridviewProducts_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridviewProducts.EditIndex = e.NewEditIndex;
            GridviewProducts.DataSource = ViewState["CurrentTable"];
            GridviewProducts.DataBind();            
        }

        protected void GridviewProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];

            GridViewRow row = GridviewProducts.Rows[e.RowIndex];
            dt.Rows[row.DataItemIndex]["Product"] = (row.FindControl("TxBxProduct") as TextBox).Text;
            dt.Rows[row.DataItemIndex]["UnitPrice"] = (row.FindControl("TxBxUnitPrice") as TextBox).Text;

            GridviewProducts.EditIndex = -1;
            GridviewProducts.DataSource = ViewState["CurrentTable"];
            GridviewProducts.DataBind();    
        }

        protected void GridviewProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridviewProducts.EditIndex = -1;
            GridviewProducts.DataSource = ViewState["CurrentTable"];
            GridviewProducts.DataBind();            
        }

        protected void GridviewProducts_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            dt.Rows.RemoveAt(e.RowIndex);

            GridviewProducts.DataSource = dt;
            GridviewProducts.DataBind();           
        }        
        
    }
}
